/*
   Copyright 2010 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.latestbit.smartobjects.examples.chat.server;

import java.util.HashMap;

import java.util.Map;

import org.latestbit.smartobjects.examples.chat.*;
import org.latestbit.smartobjects.exceptions.*;

public class ChatServiceImpl implements IChatServiceSyncSmartObject {
	
	private Map<String, ChatSessionImpl> sessions = new HashMap<String,ChatSessionImpl > ();
	
	IChatEventsCallback eventsCallback = null;

	public ChatServiceImpl (CommandLineOptions options) {
	}

	@Override
	public void destroy() {
		synchronized(sessions) {
			for(ChatSessionImpl session : sessions.values()) {
				try {
					session.getEventsCallback().onSessionWasClosedRequest( eventsCallback );
				} 
				catch (Exception e) {
					e.printStackTrace();
				}
			}		
			sessions.clear();
		}
	}

	@Override
	public ChatSessionSkeleton enterToChat( 
			String login, 
			String password, 
			IChatEvents eventCallback) 
				throws SmartBusSystemException, UnableToLoginException {
		ChatSessionSkeleton skeleton = new ChatSessionSyncSkeleton( createChatSession(login, password, eventCallback) );
		return skeleton;
	}
	
	static int loginIdx = 1;
	
	public ChatSessionImpl createChatSession( 
			String login, 
			String password, 
			IChatEvents eventCallback ) throws UnableToLoginException {
		ChatSessionImpl session = null;
		
		synchronized(sessions) {
			if(sessions.get(login)!=null) {
				login = login+" ["+ (loginIdx++) + "]";
				// throw new UnableToLoginException("Login is not unique!");
			}
			session = new ChatSessionImpl (this, login, eventCallback) ;
			sessions.put(login, session);
		}
		sendSystemMessageToAll(new ChatMessage("Entered "+login+". Welcome!"));
		return session;
	}

	public void removeChatSession(String login) {
		synchronized(sessions) {
			sessions.remove(login);
		}
		sendSystemMessageToAll ( new ChatMessage("Logout: "+login) );
	}

	public void sendMessageToAll(
			ChatSessionImpl fromChatSession,
			ChatMessage message) {
		synchronized(sessions) {
			for(ChatSessionImpl session : sessions.values()) {
				if(session!=fromChatSession) {
					try {
						session.getEventsCallback().onUserMessageRequest( fromChatSession.getLogin(), message, eventsCallback );
					} 
					catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}		
	}
	
	public void sendSystemMessageToAll ( ChatMessage message ) {
		synchronized(sessions) {
			for(ChatSessionImpl session : sessions.values()) {
				try {
					session.getEventsCallback().onSystemMessageRequest(message, eventsCallback);
				} 
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		}		
	}
	

	public void sendPrivateMessage(
			ChatSessionImpl fromChatSession,
			String login, ChatMessage message) {
		synchronized(sessions) {
			ChatSessionImpl session = sessions.get(login);
			try {
				if(session!=null) {			
					session.getEventsCallback().onUserMessageRequest ( fromChatSession.getLogin(), message, eventsCallback );
				}
				else
					fromChatSession.getEventsCallback().onSystemMessageRequest(new ChatMessage("Unable to found any users with login: "+login), eventsCallback);
			} 
			catch (Exception e) {
				e.printStackTrace();
			}			
		}		
	}

}
